#include <iostream>
#include <string>
#include <strings.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include <unistd.h>


class Consumer
{

 

 enum{SOCKETFD_ERRNO = 1,SOCKETBIND_ERRNO = 2,RECVFROM_ERRNO = 3};

public:
    Consumer(const std::string& ip = INADDR_ANY,uint16_t port = 8080)
    :_ip(ip),_port(port),_socketfd(-1)
    {}

    void initconsumer()
    {
         _socketfd = socket(AF_INET,SOCK_DGRAM,0);
        
        if(_socketfd == - 1)
        {
            std::cout << errno << strerror(errno) << std::endl;
            exit(1);
        }
        std::cout << "socket sucess : " << _socketfd << std::endl;
        //注意了客户端不需要自己链接bind，因为客户端的ip和客户端并不重要
    }

    void start()
    {
        struct sockaddr_in address;
        bzero(&address,sizeof address);
        //获取服务端信息
        address.sin_addr.s_addr = inet_addr(_ip.c_str());
        address.sin_family = AF_INET;
        address.sin_port = htons(_port);

        char buffer[1024];
        while(1)
        {
            
            std::cout << "Pelase,cin#";
            std::cin >> buffer;
            sendto(_socketfd,buffer,sizeof buffer,0,(sockaddr*)&address,sizeof address);
            //接收buffer成功，开始使用回调函数处理callback
           
        }
    }

private:
    std::string _ip;
    uint16_t _port;
    int _socketfd;
};
